package com.its.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.its.cloud.myHandler.BlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 流控测试
 * 热点测试
 * 添加Java运行异常兜底和sentinel双重配置案例
 *
 * @author huguangjun
 * @className FlowLimitController
 * @date 2021/1/15
 * @desc 13:21
 */
@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        return "-----testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "-----testB";
    }

    /**
     * 测试自定义提示返回信息
     *
     * @return
     */
    @GetMapping("/testC")
    @SentinelResource(
            value = "testC",
            blockHandlerClass = BlockHandler.class,
            blockHandler = "handlerException"
    )
    public String testC() {
        return "-----testC";
    }

    /**
     * 测试自定义提示返回信息
     *
     * @return
     */
    @GetMapping("/testD")
    @SentinelResource(
            value = "testD",
            blockHandlerClass = BlockHandler.class,
            blockHandler = "handlerException2",
            fallback = "fallbackMethod1"
            //exceptionsToIgnore = {IllegalArgumentException.class} // 假如抛此异常将不再有fallback兜底方法了（忽略异常配置）
    )// fallback 管Java异常提示，blockHandler管sentinel提示
    public String testD(@RequestParam(value = "p1", required = false) long p1) throws Exception {
        if (p1 == 4) {
            throw new IllegalArgumentException("提示参数非法异常");
        } else if (p1 == 6) {
            throw new NullPointerException("提示空指针参数异常");
        }
        return "-----testD";
    }

    public String fallbackMethod1(long p1, Throwable e) {
        return "提示Java异常" + e.getStackTrace();
    }

}
